define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'my/article_file/index?article_id=' + Config.article_id,
                    add_url: 'my/article_file/add?article_id=' + Config.article_id,
                    edit_url: 'my/article_file/edit?article_id=' + Config.article_id,
                    del_url: 'my/article_file/del?article_id=' + Config.article_id,
                    look_url: 'my/article_file/look?article_id=' + Config.article_id,
                    // multi_url: 'department/article/multi',
                    table: 'my',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                // showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false, addclass: 'file_id', formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('文件名'), width: "50%", operate: 'like'},
                        {field: 'name2', title: __('摘要'), operate: 'like'},
                        {field: 'file_content', title: __('全文检索'), operate: 'like', visible: false},
                        {field: 'user.nickname', title: __('上传用户'), operate: false},
                        {field: 'create_time', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('Operate'), width: "120px", table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'look',
                                    text: __('查看'),
                                    title: __('查看'),
                                    classname: 'btn btn-xs btn-success look',
                                    extend:'target="_blank"',
                                    visible: function () {
                                        return Config.isAuth == 1? true:false;
                                    },
                                    url: function (value, row) {
                                        return value.file_path;
                                    },
                                },
                            ],
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);


            $(document).on("click", ".look", function (e) {
                var fileId = $(this).parent().parent().find(".text-primary").text();
                var url = $.fn.bootstrapTable.defaults.extend.look_url;
                Fast.api.ajax({
                    url: url,
                    data: {file_id: fileId},
                }, function () {
                    return false;
                }, function () {
                    return false;
                });

            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});